<template>
  <div class="game-menu">
    <div class="menu-container">
      <!-- 游戏标题 -->
      <div class="title-section">
        <h1 class="game-title">Vue跑酷游戏</h1>
        <p class="game-subtitle">挑战极限，超越自我</p>
      </div>
      
      <!-- 主要按钮 -->
      <div class="main-actions">
        <button 
          class="btn btn-primary start-btn fullscreen-btn"
          @click="startFullscreenGame"
          :disabled="isLoading"
        >
          <i class="icon-fullscreen"></i>
          <span>{{ isLoading ? '加载中...' : '全屏游戏' }}</span>
        </button>
        <button 
          class="btn btn-secondary start-btn normal-btn"
          @click="startGame"
          :disabled="isLoading"
        >
          <i class="icon-play"></i>
          <span>普通模式</span>
        </button>
      </div>
      
      <!-- 次要按钮 -->
      <div class="secondary-actions">
        <router-link to="/characters" class="btn btn-secondary">
          <i class="icon-character"></i>
          <span>角色选择</span>
        </router-link>
        <router-link to="/leaderboard" class="btn btn-secondary">
          <i class="icon-trophy"></i>
          <span>排行榜</span>
        </router-link>
        <router-link to="/settings" class="btn btn-secondary">
          <i class="icon-settings"></i>
          <span>设置</span>
        </router-link>
      </div>
      
      <!-- 游戏说明 -->
      <div class="game-info">
        <div class="controls-info">
          <div class="control-item">
            <kbd>空格</kbd>
            <span>跳跃</span>
          </div>
          <div class="control-item">
            <kbd>↓</kbd>
            <span>滑铲</span>
          </div>
          <div class="control-item">
            <kbd>←→</kbd>
            <span>移动</span>
          </div>
        </div>
        <p class="game-description">收集道具，避开障碍物，创造最高分数！</p>
      </div>
    </div>
    
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="decoration-item item-1"></div>
      <div class="decoration-item item-2"></div>
      <div class="decoration-item item-3"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useGameStore } from '@/stores/gameStore'

/**
 * 游戏主菜单组件
 * 提供游戏开始、角色选择、排行榜和设置的入口
 * @component
 */

const router = useRouter()
const gameStore = useGameStore()
const isLoading = ref(false)

/**
 * 开始游戏
 * 初始化游戏状态并跳转到游戏页面
 */
const startGame = async (): Promise<void> => {
  try {
    isLoading.value = true
    
    // 重置游戏状态
    gameStore.resetGame()
    
    // 跳转到游戏页面
    await router.push('/game')
  } catch (error) {
    console.error('启动游戏失败:', error)
  } finally {
    isLoading.value = false
  }
}

/**
 * 开始全屏游戏
 * 进入全屏模式并开始游戏
 */
const startFullscreenGame = async (): Promise<void> => {
  try {
    isLoading.value = true
    
    // 进入全屏模式
    await enterFullscreen()
    
    // 重置游戏状态
    gameStore.resetGame()
    
    // 跳转到游戏页面
    await router.push('/game')
  } catch (error) {
    console.error('启动全屏游戏失败:', error)
  } finally {
    isLoading.value = false
  }
}

/**
 * 进入全屏模式
 */
const enterFullscreen = async (): Promise<void> => {
  try {
    const element = document.documentElement
    if (element.requestFullscreen) {
      await element.requestFullscreen()
    } else if ((element as any).webkitRequestFullscreen) {
      await (element as any).webkitRequestFullscreen()
    } else if ((element as any).msRequestFullscreen) {
      await (element as any).msRequestFullscreen()
    }
    console.log('已进入全屏模式')
  } catch (error) {
    console.warn('无法进入全屏模式:', error)
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/responsive.scss';

.game-menu {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 0;
  margin: 0;
  overflow: hidden;
  z-index: 1000;
  
  @include mobile-all {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    height: -webkit-fill-available;
    padding: env(safe-area-inset-top, 1rem) env(safe-area-inset-right, 1rem) env(safe-area-inset-bottom, 1rem) env(safe-area-inset-left, 1rem);
    margin: 0;
  }

  .menu-container {
    position: relative;
    z-index: 10;
    text-align: center;
    max-width: 450px;
    width: 100%;
    
    @include mobile-all {
      max-width: 350px;
    }
    
    @include mobile-small {
      max-width: 300px;
    }
  }

  // 标题区域
  .title-section {
    margin-bottom: 3rem;
    
    @include mobile-all {
      margin-bottom: 2rem;
    }

    .game-title {
      font-size: 3.5rem;
      font-weight: 900;
      color: white;
      margin-bottom: 0.5rem;
      text-shadow: 3px 3px 6px rgba(0, 0, 0, 0.4);
      letter-spacing: -1px;
      
      @include mobile-all {
        font-size: 2.8rem;
      }
      
      @include mobile-small {
        font-size: 2.2rem;
      }
    }

    .game-subtitle {
      font-size: 1.1rem;
      color: rgba(255, 255, 255, 0.9);
      margin: 0;
      font-weight: 300;
      letter-spacing: 1px;
      
      @include mobile-all {
        font-size: 1rem;
      }
      
      @include mobile-small {
        font-size: 0.9rem;
      }
    }
  }

  // 主要按钮
  .main-actions {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 2rem;
    
    @include mobile-all {
      margin-bottom: 1.5rem;
      gap: 0.75rem;
    }

    .start-btn {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 0.75rem;
      width: 100%;
      padding: 1.25rem 2rem;
      font-size: 1.3rem;
      font-weight: 700;
      border: none;
      border-radius: $border-radius-xl;
      color: white;
      cursor: pointer;
      transition: all 0.3s ease;
      text-transform: uppercase;
      letter-spacing: 1px;
      position: relative;
      overflow: hidden;
      
      @include mobile-all {
        padding: 1rem 1.5rem;
        font-size: 1.2rem;
      }
      
      @include mobile-small {
        padding: 1rem;
        font-size: 1.1rem;
      }
      
      @include touch-device {
        min-height: 56px;
        -webkit-tap-highlight-color: transparent;
        
        &:active:not(:disabled) {
          transform: scale(0.98);
        }
      }

      @include non-touch-device {
        &:hover:not(:disabled) {
          transform: translateY(-3px);
        }
      }

      &:disabled {
        opacity: 0.7;
        cursor: not-allowed;
        transform: none;
      }

      i {
        font-size: 1.4rem;
        
        @include mobile-all {
          font-size: 1.2rem;
        }
      }

      // 全屏按钮样式
      &.fullscreen-btn {
        background: linear-gradient(135deg, #FF6B35, #F7931E);
        box-shadow: 0 6px 20px rgba(255, 107, 53, 0.4);
        
        &::before {
          content: '';
          position: absolute;
          top: 0;
          left: -100%;
          width: 100%;
          height: 100%;
          background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
          animation: shimmer 2s infinite;
        }

        @include non-touch-device {
          &:hover:not(:disabled) {
            background: linear-gradient(135deg, #FF8A65, #FFB74D);
            box-shadow: 0 8px 25px rgba(255, 107, 53, 0.6);
          }
        }
        
        @include touch-device {
          &:active:not(:disabled) {
            box-shadow: 0 4px 15px rgba(255, 107, 53, 0.5);
          }
        }
      }

      // 普通模式按钮样式
      &.normal-btn {
        background: linear-gradient(135deg, #4CAF50, #45a049);
        box-shadow: 0 6px 20px rgba(76, 175, 80, 0.4);
        font-size: 1.1rem;
        padding: 1rem 2rem;
        
        @include mobile-all {
          font-size: 1rem;
          padding: 0.875rem 1.5rem;
        }

        @include non-touch-device {
          &:hover:not(:disabled) {
            background: linear-gradient(135deg, #66BB6A, #4CAF50);
            box-shadow: 0 8px 25px rgba(76, 175, 80, 0.5);
          }
        }
        
        @include touch-device {
          &:active:not(:disabled) {
            box-shadow: 0 4px 15px rgba(76, 175, 80, 0.5);
          }
        }
      }
    }
  }

  // 次要按钮
  .secondary-actions {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-bottom: 2.5rem;
    
    @include mobile-all {
      gap: 0.5rem;
      margin-bottom: 2rem;
    }

    .btn {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 0.5rem;
      padding: 0.875rem 1.5rem;
      font-size: 1rem;
      font-weight: 600;
      border: none;
      border-radius: $border-radius-lg;
      text-decoration: none;
      cursor: pointer;
      transition: all 0.3s ease;
      background: rgba(255, 255, 255, 0.15);
      color: white;
      backdrop-filter: blur(10px);
      border: 1px solid rgba(255, 255, 255, 0.2);
      
      @include mobile-all {
        padding: 0.75rem 1.25rem;
        font-size: 0.95rem;
      }
      
      @include touch-device {
        min-height: 48px;
        -webkit-tap-highlight-color: transparent;
        
        &:active {
          transform: scale(0.98);
          background: rgba(255, 255, 255, 0.25);
        }
      }

      @include non-touch-device {
        &:hover {
          transform: translateY(-2px);
          background: rgba(255, 255, 255, 0.25);
          box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        }
      }

      i {
        font-size: 1.1rem;
        
        @include mobile-all {
          font-size: 1rem;
        }
      }
    }
  }

  // 游戏信息
  .game-info {
    .controls-info {
      display: flex;
      justify-content: center;
      gap: 1rem;
      margin-bottom: 1rem;
      flex-wrap: wrap;
      
      @include mobile-all {
        gap: 0.75rem;
      }
      
      @include mobile-small {
        gap: 0.5rem;
      }

      .control-item {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        background: rgba(255, 255, 255, 0.1);
        padding: 0.5rem 0.75rem;
        border-radius: $border-radius-md;
        backdrop-filter: blur(10px);
        
        @include mobile-small {
          padding: 0.375rem 0.5rem;
        }

        kbd {
          background: rgba(255, 255, 255, 0.2);
          border: 1px solid rgba(255, 255, 255, 0.3);
          border-radius: 4px;
          padding: 0.25rem 0.5rem;
          font-size: 0.8rem;
          font-weight: bold;
          color: white;
          min-width: 28px;
          text-align: center;
          
          @include mobile-small {
            font-size: 0.7rem;
            min-width: 24px;
            padding: 0.2rem 0.4rem;
          }
        }

        span {
          font-size: 0.85rem;
          color: rgba(255, 255, 255, 0.9);
          
          @include mobile-small {
            font-size: 0.75rem;
          }
        }
      }
    }

    .game-description {
      color: rgba(255, 255, 255, 0.8);
      font-size: 0.95rem;
      line-height: 1.5;
      margin: 0;
      
      @include mobile-all {
        font-size: 0.9rem;
      }
      
      @include mobile-small {
        font-size: 0.85rem;
      }
    }
  }

  // 背景装饰
  .background-decoration {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 1;

    .decoration-item {
      position: absolute;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.1);
      animation: float 6s ease-in-out infinite;

      &.item-1 {
        width: 100px;
        height: 100px;
        top: 10%;
        left: 10%;
        animation-delay: 0s;
      }

      &.item-2 {
        width: 150px;
        height: 150px;
        top: 60%;
        right: 15%;
        animation-delay: 2s;
      }

      &.item-3 {
        width: 80px;
        height: 80px;
        bottom: 20%;
        left: 20%;
        animation-delay: 4s;
      }
    }
  }
  
  // 横屏模式优化
  @include landscape {
    @include mobile-all {
      .menu-container {
        max-width: 500px;
        
        .title-section {
          margin-bottom: 2rem;
          
          .game-title {
            font-size: 2.5rem;
          }
        }
        
        .main-actions {
          margin-bottom: 1.5rem;
        }
        
        .secondary-actions {
          display: grid;
          grid-template-columns: 1fr 1fr;
          gap: 0.75rem;
          
          .btn:last-child {
            grid-column: 1 / -1;
          }
        }
      }
    }
  }
}

// 动画
@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
    opacity: 0.1;
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
    opacity: 0.2;
  }
}

@keyframes shimmer {
  0% {
    left: -100%;
  }
  100% {
    left: 100%;
  }
}

// 安全区域适配
@supports (padding: env(safe-area-inset-top)) {
  .game-menu {
    @include mobile-all {
      padding-top: env(safe-area-inset-top, 1rem);
      padding-bottom: env(safe-area-inset-bottom, 1rem);
      padding-left: env(safe-area-inset-left, 1rem);
      padding-right: env(safe-area-inset-right, 1rem);
    }
  }
}

// 图标样式
.icon-play::before { content: '▶'; }
.icon-fullscreen::before { content: '⛶'; }
.icon-character::before { content: '👤'; }
.icon-trophy::before { content: '🏆'; }
.icon-settings::before { content: '⚙'; }
</style>